/**
 * Copyright(c) 2015 Jade Techonologies Co., Ltd.
 *
 * History:
 *   15-1-21 下午4:30 Created by lyyang
 */
package com.jade.framework.security.shiro.realm;

import java.util.Collection;
import java.util.List;

import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.Permission;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * 支持超级管理员的realm
 *
 * @author <a href="mailto:lyyang@jade.com">lyyang</a>
 * @version 1.0 15-1-21 下午4:30
 */
public abstract class SupportSupermanRealm  extends AuthorizingRealm
{
    public boolean isPermitted(PrincipalCollection principals, String permission)
    {
        return isSuperman(principals) || super.isPermitted(principals, permission);
    }

    public boolean isPermitted(PrincipalCollection principals, Permission permission)
    {
        return isSuperman(principals) || super.isPermitted(principals, permission);
    }

    public boolean[] isPermitted(PrincipalCollection principals, List<Permission> permissions)
    {
        if (isSuperman(principals)) {
            if (permissions == null || permissions.size() == 0) {
                return new boolean[0];
            }
            else {
                boolean[] result = new boolean[permissions.size()];
                for (int i = 0; i < result.length; i++) {
                    result[i] = true;
                }
                return result;
            }
        }
        return super.isPermitted(principals, permissions);
    }

    public boolean isPermittedAll(PrincipalCollection principal, Collection<Permission> permissions)
    {
        return isSuperman(principal) || super.isPermittedAll(principal, permissions);
    }

    public boolean hasRole(PrincipalCollection principal, String roleIdentifier)
    {
        return isSuperman(principal) || super.hasRole(principal, roleIdentifier);
    }

    public boolean[] hasRoles(PrincipalCollection principal, List<String> roleIdentifiers)
    {
        if (isSuperman(principal)) {
            if (roleIdentifiers == null || roleIdentifiers.size() == 0) {
                return new boolean[0];
            }
            boolean[] result = new boolean[roleIdentifiers.size()];
            for (int i = 0; i < result.length; i++) {
                result[i] = true;
            }
            return result;
        }
        else {
            return super.hasRoles(principal, roleIdentifiers);
        }
    }

    public boolean hasAllRoles(PrincipalCollection principal, Collection<String> roleIdentifiers)
    {
        return isSuperman(principal) || super.hasAllRoles(principal, roleIdentifiers);
    }

    public void checkRole(PrincipalCollection principal, String role)
            throws AuthorizationException
    {
        if (!isSuperman(principal)) {
            super.checkRole(principal, role);
        }
    }

    public void checkRoles(PrincipalCollection principal, Collection<String> roles)
            throws AuthorizationException
    {
        if (!isSuperman(principal)) {
            super.checkRoles(principal, roles);
        }
    }

    public void checkPermission(PrincipalCollection subjectIdentifier, String permission)
            throws AuthorizationException
    {
        if (!isSuperman(subjectIdentifier)) {
            super.checkPermission(subjectIdentifier, permission);
        }
    }

    public void checkPermission(PrincipalCollection principal, Permission permission)
            throws AuthorizationException
    {
        if (!isSuperman(principal)) {
            super.checkPermission(principal, permission);
        }
    }

    public void checkPermissions(PrincipalCollection subjectIdentifier, String... permissions)
            throws AuthorizationException
    {
        if (!isSuperman(subjectIdentifier)) {
            super.checkPermissions(subjectIdentifier, permissions);
        }
    }

    public void checkRoles(PrincipalCollection principal, String... roles)
            throws AuthorizationException
    {
        if (!isSuperman(principal)) {
            super.checkRoles(principal, roles);
        }
    }

    public void checkPermissions(PrincipalCollection principal, Collection<Permission> permissions)
            throws AuthorizationException
    {
        if (!isSuperman(principal)) {
            super.checkPermissions(principal, permissions);
        }
    }

    protected abstract boolean isSuperman(PrincipalCollection principal);
}
